// import com.alibaba.druid.sql.ast.SQLDataType;
// import com.alibaba.druid.sql.ast.SQLDataTypeImpl;
// import com.alibaba.druid.sql.ast.SQLObject;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor;

#include "SQLDoubleExpr.h"
#include "../SQLDataTypeImpl.h"
#include "../SQLObject.h"
#include "../../visitor/SQLASTVisitor.h"
#include "../../../utils/doubleEqual.h"

SQLDataType_ptr SQLDoubleExpr::DATA_TYPE = SQLDataType_ptr(new SQLDataTypeImpl(make_string_ptr("DOUBLE")));

SQLDoubleExpr::SQLDoubleExpr()
{
}

SQLDoubleExpr::SQLDoubleExpr(string_ptr value)
    : SQLNumericLiteralExpr()
{

  this->value = std::stod(value->c_str());
}

SQLDoubleExpr::SQLDoubleExpr(double value)
    : SQLNumericLiteralExpr()
{

  this->value = value;
}

SQLObject_ptr SQLDoubleExpr::clone()
{
  SQLObject_ptr clone_tmp = SQLDoubleExpr_ptr(new SQLDoubleExpr(value));
  SQLDoubleExpr_ptr clone_res = std::dynamic_pointer_cast<SQLDoubleExpr>(clone_tmp);
  // return new SQLDoubleExpr(value);
  return clone_res;
}

//@Override
SQLObject_list_ptr SQLDoubleExpr::getChildren()
{
  return SQLObject_list_ptr();
}

//@Override
double SQLDoubleExpr::getNumber()
{
  return value;
}

double SQLDoubleExpr::getValue()
{
  return value;
}

void SQLDoubleExpr::setValue(double value)
{
  this->value = value;
}

void SQLDoubleExpr::accept0(SQLASTVisitor_ptr visitor)
{
  visitor->visit(SharedObject(SQLDoubleExpr));
  visitor->endVisit(SharedObject(SQLDoubleExpr));
}

//@Override
bool SQLDoubleExpr::equals(Object_ptr o)
{
  if (this == o.get())
  {
    return true;
  }
  if (o == nullptr || getClass() != o->getClass())
  {
    return false;
  }

  SQLDoubleExpr_ptr that = std::dynamic_pointer_cast<SQLDoubleExpr>(o);

  return doubleEqual::AreEqual(that->value, value);
}

//@Override
int SQLDoubleExpr::hashCode()
{
  long temp = static_cast<long>(value);
  return (int)(temp ^ (temp >> 32));
}

//@Override
void SQLDoubleExpr::setNumber(double number)
{
  // if (number == null) {
  //     this->setValue(double.NaN);
  //     return;
  // }

  this->setValue(number);
}

//@Override
int SQLDoubleExpr::compareTo(SQLDoubleExpr_ptr o)
{
  if (doubleEqual::AreEqual(value, o->value))
  {
    return 0;
  }

  if (value > o->value)
  {
    return 1;
  }

  return -1;
}
